package com.watchme.es;

import com.alibaba.fastjson.JSON;
import com.watchme.es.service.ElasticService;
import com.watchme.es.service.PropertiesHelper;

import java.io.PrintStream;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

public class MyStartHandler {

    public void start(String[] args) {
        String filePath = "";
        String isWrite = "0";
        if (args.length > 0) {
            filePath = args[0];
            System.out.println("参数1：" + filePath);
        }
        if (args.length > 1) {
            isWrite = args[1];
            System.out.println("参数2：" + isWrite);
        }

        System.out.println("------------------------------------------------------------------");
        System.out.println("---------------------------- 功能菜单 ----------------------------");
        System.out.println("------------------------------------------------------------------");
        System.out.println();
        System.out.println("*    1 查询索引");
        System.out.println("*    2 同步输入到ES");
        System.out.println("*    exit 退出当前程序");
        System.out.println();
        System.out.println("------------------------------------------------------------------");
        System.out.println("--------------- 佛祖保佑 神兽护体 女神助攻 流量冲天 --------------");
        System.out.println("------------------------------------------------------------------");

        try {
            // 保存原输出流
            PrintStream ps = System.out;
            // 创建文件输出流1
            PrintStream newps = new PrintStream("./log.txt");

            PropertiesHelper helper = new PropertiesHelper(filePath);
            Properties properties = helper.getKeyValue();
            while (true) {
                System.out.print("请输入您的操作：");
                Scanner xxx = new Scanner(System.in);
                String out = xxx.next();
                System.out.println("::" + out);
                if (out != null && out.equals("1")) {
                    System.out.println("即将执行的操作是【查询索引】...");
                    if ("1".equals(isWrite)) {
                        System.out.println("使用新的输出流将log输出到 ./log.txt");
                        // 设置使用新的输出流
                        System.setOut(newps);
                    }
                    ElasticService elasticService = new ElasticService(properties);
                    List list = elasticService.selectIndex();
                    System.out.println(JSON.toJSONString(list));
                    if ("1".equals(isWrite)) {
                        // 恢复原有输出流
                        System.setOut(ps);
                    }
                }

                if (out != null && out.equals("2")) {
                    System.out.println("即将执行的操作是【导入数据到ES】......");
                    if ("1".equals(isWrite)) {
                        System.out.println("使用新的输出流将log输出到 ./log.txt");
                        // 设置使用新的输出流
                        System.setOut(newps);
                    }
                    ElasticService elasticService = new ElasticService(properties);
                    elasticService.writeMysqlDataToES();
                    if ("1".equals(isWrite)) {
                        // 恢复原有输出流
                        System.setOut(ps);
                    }
                }

                if (out != null && out.equals("exit")) {
                    System.exit(0);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
